package com.css.dzj.zfjg.modules.admforce.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.admforce.entity.JgJgxwForceActionEntity;
import com.css.dzj.zfjg.modules.admforce.service.JgJgxwForceActionService;
import com.css.dzj.zfjg.modules.adminspection.entity.JgJgxwCheckActionEntity;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 行政强制行为信息
 * @author wt
 * @date 2020\2\20 0020
 */
@RestController
@RequestMapping("jgxw/jgjgxwforceaction")
public class JgJgxwForceActionController {
    @Autowired
    private JgJgxwForceActionService forceActionService;

    @Autowired
    private ServiceUtil serviceUtil;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgJgxwForceActionEntity> forceResults = forceActionService.queryPage(pageRequest);
            baseService.logSuccess(3, "分页查询行政强制行为成功", "2");
            return R.ok().put("data", forceResults);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "分页查询行政强制行为失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){
        try {
            JgJgxwForceActionEntity forceActionEntity = forceActionService.findById(id);
            baseService.logSuccess(3, "查看行政强制行为成功", "2");
            return R.ok().put("forceActionEntity", forceActionEntity);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "查看行政强制行为失败", "2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody JgJgxwForceActionEntity forceActionEntity){
        try {
            if(StringUtils.isEmpty(forceActionEntity)) {
                return R.error("行政检查行为信息不能为空");
            }
            forceActionService.add(forceActionEntity);
            baseService.logSuccess(3, "新增行政强制行为成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "新增行政强制行为失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody JgJgxwForceActionEntity forceActionEntity){
        try {
            JgJgxwForceActionEntity oldforceActionEntity = forceActionService.findById(forceActionEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(forceActionEntity, oldforceActionEntity);
            //新增数据类型i-insert,u-update,d-delete
            forceActionEntity.setCdOperation("u");
            //修改时间
            forceActionEntity.setUpdateTime(new Date());
            //更新时间
            forceActionEntity.setGxsj(new Date());
            //来源
            forceActionEntity.setCdSource("07");
            //修改人
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                forceActionEntity.setCreateUser(userInfo.getLoginName());
            }
            forceActionService.update(forceActionEntity);
            baseService.logSuccess(3, "修改行政强制行为成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "修改行政强制行为失败", "2");
            return R.error(e.getMessage());
        }
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String Id){

        try {
            if(forceActionService.delete(Id)){
                baseService.logSuccess(3, "删除行政强制行为成功", "2");
                return R.ok("操作成功!");
            }else{
                baseService.logFailed(3, "删除行政强制行为失败", "2");
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "删除行政强制行为失败", "2");
            return R.error(e.getMessage());
        }
    }

}
